package textchorus.sparql;

import java.util.*;
import java.util.Map.Entry;

import net.n3.nanoxml.*;
import textchorus.sparql.SparqlQuery.Tag;

public class XMLParse {
	private String xmlStr;	
	private IXMLElement rootElement;
	
	public XMLParse(String str) {
		this.xmlStr = str;
		
		// with the XML string constructed, parse it into a IXMLElement
		this.rootElement = buildXML();		
	}

	private IXMLElement buildXML() {
		IXMLParser parser = null;
		try {
			parser = XMLParserFactory.createDefaultXMLParser();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		IXMLReader reader = StdXMLReader.stringReader(xmlStr);
		parser.setReader(reader);
		IXMLElement xml = null;
		try {
			xml = (IXMLElement) parser.parse();
		} catch (XMLException e) {
			e.printStackTrace();
		}

		return xml;
	}
	
	
	@SuppressWarnings("unchecked")
	public Vector<String> searchElement(HashMap criteria, Tag targetTag) {
		Enumeration<IXMLElement> enumRoot = rootElement.enumerateChildren();
		
		Vector<String> abs = new Vector<String>();
		
		while (enumRoot.hasMoreElements()) {
			IXMLElement childElementA = (IXMLElement) enumRoot.nextElement();
//			System.out.println("looking at XML element: <"+childElementA.getName()+">");
			
			if(childElementA.getName().equals("results")) {				// the 'results' tag is what we're after

				Enumeration<IXMLElement> enumChildA = childElementA.enumerateChildren();
				while (enumChildA.hasMoreElements()) {
					// childElementB represents the <result> tags found in the <results> parent node
					IXMLElement childElementB = (IXMLElement) enumChildA.nextElement();
//					System.out.println("\t<"+childElementB.getName()+">");
					
					Enumeration<IXMLElement> enumChildB = childElementB.enumerateChildren();
					while (enumChildB.hasMoreElements()) {
						// childElementC contains the <binding> tags
						IXMLElement childElementC = (IXMLElement) enumChildB.nextElement();
//						System.out.println("\t\t<"+childElementC.getName()+">");		// should be <binding>
//						System.out.println("\t\t"+childElementC.getAttribute("name",null));

						String tagStr = "";
						String valStr = "";

						// based on the search criteria, make the proper checks
						Iterator iter = criteria.entrySet().iterator();
						while(iter.hasNext()) {
							Map.Entry tag = (Entry) iter.next();
							
							tagStr = tag.getKey().toString().toLowerCase();
							valStr = tag.getValue().toString();
						}
						
						// get the <binding> attribute to match first
						if(childElementC.getAttribute("name",null).equals(tagStr)) {
//							System.out.println(tagStr);
							IXMLElement childElementD = childElementC.getChildAtIndex(0);
							
							
							if(childElementD.getContent().equals(valStr) || valStr.equals("*")) {
								
								Enumeration<IXMLElement> enumChildBind = childElementB.enumerateChildren();
								while (enumChildBind.hasMoreElements()) {
									IXMLElement childElementBind = (IXMLElement) enumChildBind.nextElement();
								
									if(childElementBind.getAttribute("name",null).equals(targetTag.toString().toLowerCase())) {
										abs.addElement(childElementBind.getChildAtIndex(0).getContent());		
									}
								}
							}
						}
					}
				}
			}
		}
		return(abs);
	}
	
	
	public String getXmlStr() {
		return xmlStr;
	}
	
}
